class ownAMap {
	constructor(map) {
		// this.map=map;
	}
	//引入地图sdk
	static MapLoader() {
		return new Promise((resolve, reject) => {
			if (typeof window.AMap === 'function') {
				resolve(window.AMap)
			} else {
				// 动态引入较大类库避免影响页面展示
				const script = document.createElement('script');
				script.type = 'text/javascript';
				script.async = true;
				script.src =
					'https://webapi.amap.com/maps?v=1.4.15&callback=initAMap&key=d986e20417ee0bdd7b6c6c7b8ba44c8a';
				// script.onload = this.initAmap.bind(this)
				script.onerror = reject;
				document.head.appendChild(script);
			}
			window.initAMap = () => {
				resolve(window.AMap)
			}
		})
	}
	//初始化地图
	static initAmap(AMap) {
		let map = new AMap.Map('amap', {
			resizeEnable: true,
			center: [116.397428, 39.90923], //地图中心点
			zoom: 13 //地图显示的缩放级别
		})
		// this.map = map;
		return map
	}
	//将中文地址转为经纬度,已知地图
	static geoCodes(addrArrShz, map) {
		var markers = [];
		var geoCodeLocation = [];
		return new Promise(resolve => {
			map.plugin('AMap.Geocoder', () => {
				var geocoder = new AMap.Geocoder({
					// city: "010", //城市设为北京，默认：“全国”
				});
				geocoder.getLocation(addrArrShz, (status, result) => {
					if (status === 'complete' && result.geocodes.length) {
						console.log(result.geocodes.length)
						for (var i = 0; i < result.geocodes.length; i += 1) {
							var marker = new AMap.Marker({
								position: result.geocodes[i].location
							});
							geoCodeLocation.push(result.geocodes[i].location)
							markers.push(marker);
						}
						resolve({
							markers,
							geoCodeLocation
						})
					}
				});
			});

		})

	}

}
export default ownAMap
